//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include <wx/dcbuffer.h>
#include "statusctrl.h"
#include "rcmdapp.h"
#include "mainwnd.h"

#define EMPT_NUM_DOTS 16
#define EMPT_OFF_H	  1
#define EMPT_OFF_V	  1
#define VERT_GUP_HEAD 3
#define VERT_GUP_TAIL 25

BEGIN_EVENT_TABLE(CStatusCtrl, wxPanel)
	EVT_RIGHT_DOWN(CStatusCtrl::OnMenu)
END_EVENT_TABLE()

CStatusCtrl::CStatusCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pos,
		const wxSize& size):
			wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL|wxBORDER_SUNKEN),
			m_btLevel(0), m_btWarningLevel(66), m_btErrorLevel(90), m_nMaxVal(0)
{
	Connect(wxEVT_PAINT, wxPaintEventHandler(CStatusCtrl::OnPaint));
	Connect(wxEVT_SIZE, wxSizeEventHandler(CStatusCtrl::OnSize));
	
	SetBackgroundStyle(wxBG_STYLE_CUSTOM);
}

CStatusCtrl::~CStatusCtrl()
{
}

void CStatusCtrl::OnSize(wxSizeEvent& event)
{
	Refresh();
}

void CStatusCtrl::OnMenu(wxMouseEvent& event)
{
	wxPoint pt;
	pt.x = event.m_x;
	pt.y = event.m_y;
	
	wxGetApp().GetMainWnd()->OnPopUpMenu( ClientToScreen(pt), GetParent() );
}

void CStatusCtrl::OnPaint(wxPaintEvent& event)
{
	wxSize szMe = GetClientSize();
	wxBufferedPaintDC dc(this);

	//Clear
	dc.SetBackground(*wxBLACK_BRUSH);
	dc.Clear();
	
	//Center;
	wxCoord wBar = 2*EMPT_NUM_DOTS+1;
	wxCoord hPos = (szMe.GetX()-wBar)/2;
	wxCoord vPos = szMe.GetY()-VERT_GUP_TAIL;
	int nBarsNum = (szMe.GetY()-VERT_GUP_HEAD-VERT_GUP_TAIL)/3;
	//wxColor color(128,0,0);
	wxColour green(0,255,0);
	wxColour yellow(255,255,0);
	wxColour red(255,0,0);
	wxColor& color = green;
	
	double prcntPerBar = 100.0/nBarsNum;
	double curLvl;
	
	for (int i=0;i<nBarsNum;i++)
	{
		curLvl = i*prcntPerBar; 
		
		if ( m_btLevel>=curLvl)
		{
			if ( curLvl>=m_btWarningLevel) color = yellow;
			if ( curLvl>=m_btErrorLevel)   color = red;
			
			DrawFilledRect(hPos, vPos-i*3, dc, color); DrawFilledRect(hPos+EMPT_NUM_DOTS+1, vPos-i*3, dc, color);
		}
		else
		{
			DrawEmptyRect(hPos, vPos-i*3, dc ); DrawEmptyRect(hPos+EMPT_NUM_DOTS+1, vPos-i*3, dc );
		}
	}
	
	
	wxString txt;
	txt = txt.Format(wxT("%u %%"), m_btLevel);
	DrawText(txt, hPos, vPos, dc);
}

///////////////////////////////////////////////////////////////////////////////////
//16x2
void CStatusCtrl::DrawEmptyRect(wxCoord x, wxCoord y, class wxBufferedPaintDC& dc )
{
	wxCoord ptX, ptY;
	
	wxColour 	green(0,128,0);
	wxPen		pen(green);
	dc.SetPen(pen);
	
	for (int i=0;i<EMPT_NUM_DOTS;i++)
	{
		if (i%2)
		{
			ptX = x+i*EMPT_OFF_H;
			ptY = y+EMPT_OFF_V;
		}
		else
		{
			ptX = x+i*EMPT_OFF_H;
			ptY = y;
		}

		dc.DrawPoint(ptX, ptY);
	}
}

void CStatusCtrl::DrawFilledRect(wxCoord x, wxCoord y, class wxBufferedPaintDC& dc,
		class wxColor& color )
{
	wxBrush br(color);
	wxPen   pen(color);
	
	dc.SetPen(pen);
	dc.SetBrush(br);
	
	dc.DrawRectangle(x, y, EMPT_NUM_DOTS, 2);
}

void CStatusCtrl::DrawText(const wxString& text, wxCoord x, wxCoord y,  class wxBufferedPaintDC& dc)
{
	wxCoord wTotal=2*EMPT_NUM_DOTS+1;
	wxCoord w, h, xStart, yStart;
	wxFont  font(8, wxFONTFAMILY_DEFAULT,wxFONTSTYLE_NORMAL,wxFONTWEIGHT_NORMAL|wxFONTFLAG_ANTIALIASED);
	wxColour green(0,255,0);
	wxColour yellow(255,255,0);
	wxColour red(255,0,0);
	wxColor& color = green;
	
	dc.SetTextForeground(color);
	dc.SetFont(font);
	
	if ( m_btLevel>=m_btWarningLevel) color = yellow;
	if ( m_btLevel>=m_btErrorLevel)   color = red;
	
	dc.GetTextExtent(text, &w, &h);
	
	xStart = x+(wTotal-w)/2;
	yStart = y+(VERT_GUP_TAIL-h)/2;
	
	dc.SetTextForeground(color);
	dc.SetTextBackground(*wxBLACK);
	
	dc.DrawText(text, xStart, yStart);
}


